package com.yyn.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yyn.JwtUtil;
import com.yyn.MD5;
import com.yyn.educenter.entity.UcenterMember;
import com.yyn.educenter.entity.dto.RegisterDto;
import com.yyn.educenter.mapper.UcenterMemberMapper;
import com.yyn.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyn.servicebase.exceptionhandler.CustomException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author yyn
 * @since 2023-01-04
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public String login(UcenterMember ucenterMember) {
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();
//        判断为空
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new CustomException(20001, "用户名或密码为空");
        }
        LambdaQueryWrapper<UcenterMember> ucenterMemberLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ucenterMemberLambdaQueryWrapper.eq(UcenterMember::getMobile, mobile);
        UcenterMember ucenterMemberOne = this.getOne(ucenterMemberLambdaQueryWrapper);
        if (ucenterMemberOne == null) {
            throw new CustomException(20001, "手机号错误");
        }
//        判断密码
//        用MD5加密再放到数据库
        if (!MD5.encrypt(password).equals(ucenterMemberOne.getPassword())) {
            throw new CustomException(20001, "密码错误");
        }
        //            写token(最终是存到cookie中 给前端进行显示的)
        String jwtToken = JwtUtil.getJwtToken(ucenterMemberOne.getId(), ucenterMemberOne.getNickname());
        return jwtToken;
    }

    @Override
    public boolean register(RegisterDto registerDto) {
//        1. 非空判断
//        2. 判断验证码 （验证码在redis）
//        3. 判断手机号是否重复（已注册）
//        4. 放入数据库 （密码要加密）
        String nickname = registerDto.getNickname();
        String password = registerDto.getPassword();
        String mobile = registerDto.getMobile();
        String code = registerDto.getCode();
        if (StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password) || StringUtils.isEmpty(mobile)) {
            throw new CustomException(20001, "手机号或密码输入错误");
        }
        if (StringUtils.isEmpty(code)) {
            throw new CustomException(20001, "验证码输入错误");
        }
        LambdaQueryWrapper<UcenterMember> registerDtoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        registerDtoLambdaQueryWrapper.eq(UcenterMember::getMobile, mobile);
        int count = this.count(registerDtoLambdaQueryWrapper);
        if (count > 0) throw new CustomException(20001, "该手机号已注册");
        String s = String.valueOf(redisTemplate.opsForValue().get(mobile));
        if (!code.equals(s)) {
            throw new CustomException(20001, "验证码错误");
        }
//        正确
        UcenterMember ucenterMember = new UcenterMember();
        registerDto.setPassword(MD5.encrypt(password));
        BeanUtils.copyProperties(registerDto, ucenterMember);
        ucenterMember.setAvatar("");//默认头像
        ucenterMember.setIsDeleted(false);//默认不禁用用户
        this.save(ucenterMember);//保存
        return true;
    }

    @Override
    public UcenterMember getMemberInfo(HttpServletRequest request) {
        String memberId = JwtUtil.getMemberIdByJwtToken(request);//获取到id
        UcenterMember member = this.getById(memberId);
        return member;

    }

    @Override
    public UcenterMember getUcenterMemberByOpenid(String openid) {
        LambdaQueryWrapper<UcenterMember> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UcenterMember::getOpenid, openid);
        UcenterMember ucenterMember = this.getOne(lambdaQueryWrapper);
        return ucenterMember;


    }

    @Override
    public UcenterMember getMemberInfoByToken(String token) {
        String memberId = JwtUtil.getMemberIdByToken(token);//获取到id
        UcenterMember member = this.getById(memberId);
        return member;
    }
}
